package com.wandian.cqcsk.dialog;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.makeramen.roundedimageview.RoundedImageView;
import com.mylib.libcore.AppContext;
import com.mylib.libcore.bean.UserInfoBean;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.utils.GlideUtil;
import com.mylib.libcore.utils.QRCodeUtil;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.cqcsk.R;

import static com.mylib.libcore.config.AppSPKeyConfig.INVITE_CODE_RUL;


/**
 * 首页二维码弹框
 */
public class HomeEWMDialog extends PopupWindow {

    private ImageView iv_close;
    private View mMenuView;
    private UserInfoBean userInfoBean;
    public DialogViewClink dialogViewClink;
    public interface  DialogViewClink{
        void viewClink(boolean isSign);
    }

    public HomeEWMDialog(Context context, DialogViewClink dialogViewClink) {
        super(context);
        this.dialogViewClink =dialogViewClink;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.dialog_home_ewm, null);
        userInfoBean= AppContext.getInstance().getAppPreferencesHelper().getUserInfoBean();
        TextView tv_user_name = (TextView)mMenuView.findViewById(R.id.tv_user_name);
        tv_user_name.setText(userInfoBean.getNickName()+"");
        TextView tv_user_id =(TextView)mMenuView.findViewById(R.id.tv_user_id);
        tv_user_id.setText("邀请码:"+ userInfoBean.getInvitationCode()+"");
        RoundedImageView iv_user_head = (RoundedImageView) mMenuView.findViewById(R.id.iv_user_head);
        GlideUtil.intoHeadImg(context, AppContext.getInstance().getAppPreferenceForever().getKeyValue("userHeadImg"),iv_user_head);
        ImageView iv_ewm = mMenuView.findViewById(R.id.iv_ewm);
        String invite_url = AppContext.getInstance().getAppPreferenceForever().getKeyValue(INVITE_CODE_RUL)+"";
        if(!UIUtils.isEmpty(invite_url)){
            final Bitmap bitmap = QRCodeUtil.createQRImage(invite_url, 120, 120, null, null);
            if(!UIUtils.isEmpty(bitmap)){
                iv_ewm.setImageBitmap(bitmap);
            }
        }

        iv_close = (ImageView)mMenuView.findViewById(R.id.iv_close);
        iv_close.setOnClickListener(v ->  dismiss());

        TextView tv_login_out = (TextView)mMenuView.findViewById(R.id.tv_login_out);
        tv_login_out.setOnClickListener(v -> {
            AppContext.getInstance().getAppPreferencesHelper().clearPreferencesHelper();
            ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_LOGIN).navigation();
        });
        TextView tv_invite = (TextView)mMenuView.findViewById(R.id.tv_invite);
        tv_invite.setOnClickListener(v ->{
            dismiss();
            ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_INVITE).navigation();
        }  );


        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            this.setClippingEnabled(false);
        }

        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
//        //设置SelectPicPopupWindow弹出窗体动画效果
//        this.setAnimationStyle(R.style.popwin_anim_style);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);


    }

    @Override
    public void showAsDropDown(View anchor) {
        if(Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff) {
        if(Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor, xoff, yoff);
    }

}